package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.SRescActDao;
import com.fdb.efp.nls.service.dao.SResourceDao;
import com.fdb.efp.nls.service.dao.SRoleRescRuleDao;
import com.fdb.efp.nls.service.dao.SUserRoleDao;
import com.fdb.efp.nls.service.domain.SRescAct;
import com.fdb.efp.nls.service.domain.SResource;
import com.fdb.efp.nls.service.domain.SUserRole;
import com.fdb.efp.nls.service.facade.SResourceService;
import com.fdb.efp.nls.service.vo.SRescActVO;
import com.fdb.efp.nls.service.vo.SResourceVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 系统资源信息表
 * <p>
 * 
 * @author fuzm
 * @date 2018-05-03
 */
@Service("sResourceService")
public class SResourceServiceImpl implements SResourceService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(SResourceServiceImpl.class);

	@Autowired
	private SResourceDao sResourceDao;

	@Autowired
	private SRescActDao sRescActDao;

	@Autowired
	private SRoleRescRuleDao sRoleRescRuleDao;

	@Autowired
	private SUserRoleDao sUserRoleDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertSResource(SResourceVO inSResourceVo) {
		logger.debug("当前新增数据为:" + inSResourceVo.toString());
		int num = 0;
		try {
			SResource sResource = new SResource();
			beanCopy(inSResourceVo, sResource);
			SResource temp = sResourceDao.queryByPk(sResource);
			if (null == temp)
				num = sResourceDao.insertSResource(sResource);
			else
				num = -2;
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(SResourceVO inSResourceVo) {
		logger.debug("当前删除数据条件为:" + inSResourceVo);
		int num = 0;

		try {
			SResource sResource = new SResource();
			beanCopy(inSResourceVo, sResource);
			num = sResourceDao.deleteByPk(sResource);
			// 需要删除 资源操作信息 和 角色对应资源操作信息
			num += sRescActDao.deleteByRescCode(inSResourceVo.getRescCode());
			num += sRoleRescRuleDao.deleteByRescCode(inSResourceVo.getRescCode());
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSResourceVo + "删除的数据条数为" + num);
		return num;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SRescActVO> queryAllByRescCode(String rescCode) {

		logger.debug("当前查询资源下的所以资源操作码的参数信息为:" + rescCode);
		List<SRescAct> sRescActs = sRescActDao.queryAllByRescCode(rescCode);
		logger.debug("当前查询资源下的所以资源操作码的结果集数量为:" + sRescActs.size());
		List<SRescActVO> list = null;
		try {
			list = (List<SRescActVO>) beansCopy(sRescActs, SRescActVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(SResourceVO inSResourceVo) {
		logger.debug("当前修改数据为:" + inSResourceVo.toString());
		int num = 0;
		try {
			SResource sResource = new SResource();
			beanCopy(inSResourceVo, sResource);
			num = sResourceDao.updateByPk(sResource);
			if (num > 0)
				num += sRescActDao.updateRescActByRescCode(sResource);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSResourceVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public SResourceVO queryByPk(SResourceVO inSResourceVo) {

		logger.debug("当前查询参数信息为:" + inSResourceVo);
		try {
			SResource querySResource = new SResource();
			beanCopy(inSResourceVo, querySResource);
			SResource queryRslSResource = sResourceDao.queryByPk(querySResource);
			if (Objects.nonNull(queryRslSResource)) {
				SResourceVO outSResourceVo = beanCopy(queryRslSResource, new SResourceVO());
				logger.debug("当前查询结果为:" + outSResourceVo.toString());
				return outSResourceVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SResourceVO> queryAllOwner(SResourceVO sResourceVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SResource sResource = beanCopy(sResourceVo, new SResource());
		List<SResourceVO> list = null;
		try {
			List<SResource> sResources = sResourceDao.queryAllOwnerByPage(sResource);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sResources.size());
			pageSet(sResources, sResource);
			list = (List<SResourceVO>) beansCopy(sResources, SResourceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SResourceVO> queryAllCurrOrg(SResourceVO sResourceVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SResource sResource = beanCopy(sResourceVo, new SResource());
		List<SResource> sResources = sResourceDao.queryAllCurrOrgByPage(sResource);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sResources.size());
		List<SResourceVO> list = null;
		try {
			pageSet(sResources, sResource);
			list = (List<SResourceVO>) beansCopy(sResources, SResourceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SResourceVO> queryAllCurrDownOrg(SResourceVO sResourceVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SResource sResource = beanCopy(sResourceVo, new SResource());
		List<SResource> sResources = sResourceDao.queryAllCurrDownOrgByPage(sResource);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sResources.size());
		List<SResourceVO> list = null;
		try {
			pageSet(sResources, sResource);
			list = (List<SResourceVO>) beansCopy(sResources, SResourceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SResourceVO> queryChildrens(SResourceVO sResourceVo) {
		logger.debug("查询 当前 资源下的 所有子节点");
		SResource sResource = beanCopy(sResourceVo, new SResource());
		List<SResource> sResources = sResourceDao.queryChildrens(sResource);
		logger.debug("查询 当前 资源下的 所有子节点的结果集数量为:" + sResources.size());
		List<SResourceVO> list = null;
		try {
			pageSet(sResources, sResource);
			list = (List<SResourceVO>) beansCopy(sResources, SResourceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SResourceVO> queryRescAll(SResourceVO sResourceVo) {
		logger.debug("查询 当前 法人机构码 和 公共的 所有资源");
		SResource sResource = beanCopy(sResourceVo, new SResource());
		List<SResource> sResources = sResourceDao.queryRescAll(sResource);
		logger.debug("查询 当前 法人机构码 和 公共的 所有子节点的结果集数量为:" + sResources.size());
		List<SResourceVO> list = null;
		try {
			pageSet(sResources, sResource);
			list = (List<SResourceVO>) beansCopy(sResources, SResourceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	/**
	 * 新增操作
	 */
	@Override
	public int insertSRescAct(SRescActVO inSRescActVo) {
		logger.debug("当前新增数据为:" + inSRescActVo.toString());
		int num = 0;
		try {
			SRescAct sRescAct = new SRescAct();
			beanCopy(inSRescActVo, sRescAct);
			num = sRescActDao.insertSRescAct(sRescAct);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(SRescActVO inSRescActVo) {
		logger.debug("当前删除数据条件为:" + inSRescActVo);
		int num = 0;
		try {
			SRescAct sRescAct = new SRescAct();
			beanCopy(inSRescActVo, sRescAct);
			num = sRescActDao.deleteByPk(sRescAct);
			num += sRoleRescRuleDao.deleteByRescCodeRescActCode(sRescAct.getRescCode(), sRescAct.getRescActDesc());
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRescActVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(SRescActVO inSRescActVo) {
		logger.debug("当前修改数据为:" + inSRescActVo.toString());
		int num = 0;
		try {
			SRescAct sRescAct = new SRescAct();
			beanCopy(inSRescActVo, sRescAct);
			num = sRescActDao.updateByPk(sRescAct);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRescActVo + "修改的数据条数为" + num);
		return num;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SResourceVO> queryUserRescData(String userCode) {
		logger.debug("查询当前登录用户拥有的资源菜单" + userCode);
		List<SResource> sResources = null;
		if (checkR0000(userCode)){
			sResources= sResourceDao.queryUserRescDataFinal(userCode);
		}else{
			sResources = sResourceDao.queryUserRescData(userCode);
		}
		logger.debug("查询当前登录用户拥有的结果集数量为:" + sResources.size());
		List<SResourceVO> list = null;
		try {
			list = (List<SResourceVO>) beansCopy(sResources, SResourceVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	private boolean checkR0000(String userCode){
		List<SUserRole> list = sUserRoleDao.queryByUserCode(userCode);
		for (SUserRole item : list){
			if ("R0000".equals(item.getRoleCode())){
				return true;
			}
		}
		return false;
	}
}
